﻿package auto.main;



import java.util.List;
import java.util.Random;

import org.apache.ibatis.session.SqlSession;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.service.DriverService;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;

import baseDao.IpDao;
import baseDao.RecordDao;
import deta.Slide;
import entity.Ip;
import entity.Record;
import swing.Login;
import util.GetSqlSession;
import util.IPAddress;
/**
 * 
 * @author 你的网址中的广告点击
 *
 */
public class Test2 {

	public void selectAdv(WebDriver driver, DriverService service,String youUrl,String switchingIP) throws Exception {
		/*Random random3 = new Random();
		int nextInt3 = random3.nextInt(11);
		// 让浏览器访问指定的地址
		String youUrls="";
		if(nextInt3>0){
			youUrls=youUrl+ "/detail/"+nextInt3+".html";
		}else{
			youUrls=youUrl+ "/index.html";
		}
		System.out.println(youUrls);*/
		driver.get(youUrl);
		// System.out.println(adv.getUrl() + "/detail/" + adv.getPageId() +
		// ".html");
		
		// 等待页面加载完毕
		(new WebDriverWait(driver, 200)).until(new ExpectedCondition<Boolean>() {
			
			@Override
			public Boolean apply(WebDriver d) {
				// return d.getTitle().toLowerCase().startsWith("九云IT");
				// System.out.println(d.);
				
				return d.getCurrentUrl().contains(youUrl);
			}
		});
		
		Slide.slideStrip(driver, "25");
		 String youUrls="";
		if(driver.findElements(By.tagName("object")).size()==0){
			
		//找点击列表
		List<WebElement> div = driver.findElements(By.tagName("div"));
		 WebElement webElement = div.get(0);
		
		 List<WebElement> find2 = webElement.findElements(By.tagName("a"));
		 Random randoma = new Random();
			int nextInta = randoma.nextInt(find2.size());
			Thread.sleep(1000);
			youUrls = find2.get(nextInta).getAttribute("href");
			((JavascriptExecutor)driver).executeScript("arguments[0].click();",find2.get(nextInta));
			
			
			// 等待页面加载完毕
			(new WebDriverWait(driver, 200)).until(new ExpectedCondition<Boolean>() {
				
				@Override
				public Boolean apply(WebDriver d) {
					// return d.getTitle().toLowerCase().startsWith("九云IT");
					// System.out.println(d.);
					return d.getCurrentUrl().contains(youUrl);
				}
			});
			Slide.slideStrip(driver, "25");
			
		}else{
			 youUrls =youUrl;
		}
			
		
			
			
		//找iframe	
		Random co = new Random();
//		if (next3 == 1) {
			// 找到指定的iframe
			List<WebElement> iframe = driver.findElements(By.tagName("iframe"));
			Random random2 = new Random();
			int nextInt2 = random2.nextInt(iframe.size());
			if (iframe.size() > 0) {
				System.out.println("找iframe");
					WebDriver frame = driver.switchTo().frame(iframe.get(nextInt2));
					List<WebElement> a = frame.findElements(By.cssSelector("a.clk"));
					Thread.sleep(1000);
//					if (nextInt2 == i) {
						while(true){
							Random random = new Random();
							System.out.println(a.size() + "---iframe中有多少个a标签"+nextInt2+"下标的iframe");
							int nextInt = random.nextInt(a.size());
							System.out.println("rNumiframe>a:" + nextInt);
							try {
								
								((JavascriptExecutor)driver).executeScript("arguments[0].click();", a.get(nextInt));
								SqlSession session=GetSqlSession.getSession();
								//System.out.println(a.get(nextInt));
								if("vpn".equals(switchingIP)){
									
									IpDao dao = session.getMapper(IpDao.class);
									Ip ipBean=new Ip();
									ipBean.setDate(System.currentTimeMillis());
									ipBean.setOutIp(IPAddress.outIp);
									ipBean.setLocalIp(new IPAddress().localIp());
									dao.add(ipBean);
									session.commit();
								
								}
								
								
								
								
								
								//记录***************
								RecordDao recordDao = session.getMapper(RecordDao.class);
								
								Record record = new	Record();
								record.setWordid(null);
								record.setWebUrl(youUrls);
								record.setState(1);//默认成功
								record.setUserId(Login.getUser().getId());
								record.setTaskId(Test.id);//任务表中的ID
								record.setTime(System.currentTimeMillis()+"");
								System.out.println(System.currentTimeMillis()+"*********");
								record.setWord(a.get(nextInt).getAttribute("title"));
								System.out.println(a.get(nextInt).getAttribute("title")+"词-----------");
								recordDao.add(record);
								session.commit();
								session.close();
								
							} catch (Exception e) {
								e.printStackTrace();
								break;
							}
							Thread.sleep(1000);
							if(driver.getWindowHandles().size()>1){
								break;
							}
//						}
					}
					// 退出iframe
//					try {
//						System.out.println("1111");
//						driver.switchTo().defaultContent();
//					} catch (Exception e) {
//						e.printStackTrace();
//					}
					
			} else {
				System.out.println("找全部a1");
				List<WebElement> findElements = driver.findElements(By.tagName("a"));
				System.out.println("a1.size:" + findElements.size());
				Random rd = new Random();
				int rdNum = rd.nextInt(findElements.size() - 11);
				System.out.println("a1RNum:" + rdNum);
				for (int i = 11; i < findElements.size(); i++) {
					if (rdNum == i) {
						findElements.get(i).click();
					}
				}
			}
	}
	/*public static void main(String[] args) {
		SqlSession session=GetSqlSession.getSession();
		IpDao dao = session.getMapper(IpDao.class);
		Ip ipBean=new Ip();
		ipBean.setDate(System.currentTimeMillis()-1000*60*30);
		ipBean.setIpAddress("106.19.58.94");
		int i = dao.selectByTime(ipBean);
		System.out.println(i);
		session.commit();
		session.close();
	}*/
}
